# -*- coding:utf-8 -*-
#! python3

from facecat import *
import timer
import random

#更新悬浮状态
#views:视图集合
def updateView(views):
	updateViewDefault(views)
	for i in range(0,len(views)):
		view = views[i]
		if view.viewName == "price":
			view.columns[0].width = view.size.cx

#绘制视图
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaint(view, paint, clipRect):
	onPaintDefault(view, paint, clipRect) 

#绘制视图边线
#view:视图
#paint:绘图对象
#clipRect:区域
def onPaintBorder(view, paint, clipRect):
	onPaintBorderDefault(view, paint, clipRect)

#视图的鼠标移动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseMove(view, mp, buttons, clicks, delta):
	onMouseMoveDefault(view, mp, buttons, clicks, delta)
		
#视图的鼠标按下方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseDown(view, mp, buttons, clicks, delta):
	onMouseDownDefault(view, mp, buttons, clicks, delta)

#视图的鼠标抬起方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseUp(view, mp, buttons, clicks, delta):
	onMouseUpDefault(view, mp, buttons, clicks, delta)

#视图的鼠标点击方法
#view 视图
#firstTouch:是否第一次触摸 
#firstPoint:第一次触摸的坐标 
#secondTouch:是否第二次触摸 
#secondPoint:第二次触摸的坐标
#clicks 点击次数
def onClick(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks):
	onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks)

#视图的鼠标滚动方法
#view 视图
#mp 坐标
#buttons 按钮 0未按下 1左键 2右键
#clicks 点击次数
#delta 滚轮值
def onMouseWheel(view, mp, buttons, clicks, delta):
	onMouseWheelDefault(view, mp, buttons, clicks, delta)

#绘制列 
#grid:表格 
#column:列
#paint:绘图对象 
#left:左侧坐标 
#top:上方坐标 
#right:右侧坐标 
#bottom:下方坐标
def onPaintGridColumn(grid, column, paint, left, top, right, bottom):
	width = right - left
	height = bottom - top
	font2 = "Default,14"
	tSize = paint.textSize("列1", font2)
	if grid.paint.defaultUIStyle == "dark":
		paint.fillRect("rgb(0,0,0)", left, top, right, bottom)
		paint.drawText("列1", "rgb(200,200,200)", font2, left + 5, top + height / 2 - tSize.cy / 2)
		paint.drawText("列2", "rgb(200,200,200)", font2, left + width * 0.4 + 5, top + height / 2 - tSize.cy / 2)
		paint.drawText("列3", "rgb(200,200,200)", font2, left + width * 0.7 + 5, top + height / 2 - tSize.cy / 2)
	elif grid.paint.defaultUIStyle == "light":
		paint.fillRect("rgb(255,255,255)", left, top, right, bottom)
		paint.drawText("列1", "rgb(50,50,50)", font2, left + 5, top + height / 2 - tSize.cy / 2)
		paint.drawText("列2", "rgb(50,50,50)", font2, left + width * 0.4 + 5, top + height / 2 - tSize.cy / 2)
		paint.drawText("列3", "rgb(50,50,50)", font2, left + width * 0.7 + 5, top + height / 2 - tSize.cy / 2)

#绘制单元格 
#grid:表格 
#row:行 
#column:列 
#cell:单元格
#paint:绘图对象 
#left:左侧坐标 
#top:上方坐标 
#right:右侧坐标 
#bottom:下方坐标
def onPaintGridCell(grid, row, column, cell, paint, left, top, right, bottom):
	width = right - left
	height = bottom - top
	baseUpper = "Code1"
	font1 = "Default,16"
	font2 = "Default,14"
	font3 = "Default,12"
	tSize = paint.textSize(baseUpper, font1)
	quoteUpper = " / " + "Code2"
	strVolume = toFixed(cell.volume, 6)
	strPrice = toFixed(cell.close, 6)
	tSize2 = paint.textSize(strVolume, font3)
	tSize3 = paint.textSize(strPrice, font2)
	strPrice2 = toFixed(cell.close * 7.24, 6)
	diffRange = toFixed((cell.close - cell.open) / cell.close * 100, 2) + "%"
	if grid.paint.defaultUIStyle == "dark":
		paint.drawText(baseUpper, "rgb(255,255,255)", font1, left + 5, top + height / 2 - tSize2.cy)
		paint.drawText(quoteUpper, "rgb(200,200,200)", font3, left + 5 + tSize.cx, top + height / 2 - tSize2.cy)
		paint.drawText(strVolume, "rgb(200,200,200)", font3, left + 5, top + height / 2)
	elif grid.paint.defaultUIStyle == "light":
		paint.drawText(baseUpper, "rgb(0,0,0)", font1, left + 5, top + height / 2 - tSize2.cy)
		paint.drawText(quoteUpper, "rgb(50,50,50)", font3, left + 5 + tSize.cx, top + height / 2 - tSize2.cy)
		paint.drawText(strVolume, "rgb(50,50,50)", font3, left + 5, top + height / 2 )
	tSize5 = paint.textSize("100000.00%", font1);
	colRect = FCRect(left + width * 0.7 + 5, top + height / 2 - tSize5.cy / 2, left + width * 0.7 + 5 + tSize5.cx, top + height / 2 + tSize5.cy / 2)
	color = "rgb(15,193,118)"
	if cell.close >= cell.open:
		color = "rgb(219,68,83)"
	paint.drawText(strPrice, color, font2, left + width * 0.4 + 5, top + height / 2 - tSize3.cy)
	paint.drawText(strPrice2, color, font2, left + width * 0.4 + 5, top + height / 2)
	paint.fillRect(color, colRect.left, colRect.top, colRect.right, colRect.bottom)
	tSize4 = paint.textSize(diffRange, font1)
	if grid.paint.defaultUIStyle == "dark":
		paint.drawText(diffRange, "rgb(255,255,255)", font1, left + width * 0.7 + 5 + tSize5.cx / 2 - tSize4.cx / 2, top + height / 2 - tSize4.cy / 2)
	elif grid.paint.defaultUIStyle == "light":
		paint.drawText(diffRange, "rgb(0,0,0)", font1, left + width * 0.7 + 5 + tSize5.cx / 2 - tSize4.cx / 2, top + height / 2- tSize4.cy / 2)

appPaint = FCPaint() #创建绘图对象
appPaint.defaultUIStyle = "dark"
#注册事件
appPaint.onPaint = onPaint 
appPaint.onPaintBorder = onPaintBorder
appPaint.onMouseDown = onMouseDown 
appPaint.onMouseMove = onMouseMove 
appPaint.onMouseUp = onMouseUp
appPaint.onMouseWheel = onMouseWheel
appPaint.onClick = onClick
appPaint.onPaintGridCell = onPaintGridCell
appPaint.onPaintGridColumn = onPaintGridColumn
appPaint.onUpdateView = updateView

#消息循环
def WndProc(hwnd,msg,wParam,lParam):
	return WndProcDefault(appPaint,hwnd,msg,wParam,lParam)

#创建窗体
createMainWindow(appPaint, "facecat-py", WndProc)

#检查数据
def checkNewData(a='', b=''):
	invalidate(appPaint)

#启动秒表
timer.set_timer(50, checkNewData)
#创建列表
priceList = FCGrid()
priceList.viewName = "price"
priceList.backColor = "rgb(0,0,0)"
priceList.scrollBarColor = "rgb(100,100,100)"
#设置行高
priceList.rowHeight = 50
#设置列头的高度
priceList.headerHeight = 20
#添加到视图
addView(priceList, appPaint)
#填充布局
priceList.dock = "fill"
#创建和添加列
column1 = FCGridColumn()
column1.text = "column"
column1.width = 500
priceList.columns.append(column1)
for i in range(0, 100):
	row = FCGridRow()
	priceList.rows.append(row)
	cell = FCGridCell()
	cell.open = random.randint(10,100);
	cell.close = random.randint(10,100);
	cell.volume = random.randint(10,1000);
	cell.update = 0
	row.cells.append(cell)
	
#获取窗体大小
rect = win32gui.GetClientRect(appPaint.hWnd)
#更新布局
appPaint.size = FCSize(rect[2] - rect[0], rect[3] - rect[1])
updateView(appPaint.views)
showWindow(appPaint)